import {Component, OnInit, ViewEncapsulation, ViewChild} from '@angular/core';
import { Router} from "@angular/router";
import {GoodsTicketService} from "@goods/goods-ticket/goods-ticket.service";
import {Content} from "@shared/components/toolbar/toolbar-content";
import {SeedInfiniteScroll} from "@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {TicketDetailVO} from "../../../../interface/goods/goods.model";

@Component({
  selector: 'seed-goods-ticket-list',
  templateUrl: './goods-ticket-list.component.html',
  styleUrls: ['./goods-ticket-list.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class GoodsTicketListComponent implements OnInit {
  public ticketsList: Array <TicketDetailVO> = [];
  public store = {
    hasData: true
  };
  public params = {
    page: 1,
    rows: 10,
  };
  public  goodsCode;
  @ViewChild(Content) content;
  @ViewChild(SeedInfiniteScroll) infiniteScroll;

  constructor(
    private goodsTicketService: GoodsTicketService,
    public routerTo: Router,
  ) {
  }

  /**
   * 获取票务服务列表
   * @author zhoukf1349
   * @date 2018-04-03
   */
  getGoodsTicketList() {
    this.goodsTicketService.getGoodsTicketList(this.params).subscribe(res => {
      if (res.result === '0' && res.data && res.data.list.length > 0) {
        console.log(res.data);
        this.params.page++;
        if (this.infiniteScroll) {
          this.infiniteScroll.complete();
        }
        this.ticketsList = [...this.ticketsList, ...res.data.list];
        if (this.ticketsList.length === 0) {
          this.store.hasData = false;
        }
        if (!res.data.hasNextPage && this.infiniteScroll) {
          this.infiniteScroll.enable(false);
        }
      }
    });
  }
  /**
   * 前往服务商品详情
   * @param goodsCode 商品编码
   */
  goToTicketDetailPage(goodsCode: string) {
      if (goodsCode) {
        this.routerTo.navigateByUrl(`/goods/p/${goodsCode}`);
    }
  }

  ngOnInit() {
    this.getGoodsTicketList();
  }
}
